/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 孙炯
 * <p>
 * 文件名:SysSsoLogDaoImpl.java
 * <p>
 * 类产生时间: 2012-6-15 上午9:32:54
 * <p>
 * 负责人: 孙炯
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.ISysSsoLogDao;
import com.oceansoft.core.dao.mapper.SysSsoLogMapper;
import com.oceansoft.core.domain.SysSsoLog;

/**
 * 系统访问日志管理接口实现类
 * 
 * @author sunj
 * 
 */
@Repository
public class SysSsoLogDaoImpl implements ISysSsoLogDao {
	@Resource
	private SimpleJdbcTemplate sjt;

	@Override
	public Pagination<SysSsoLog> findSsoInfo(int pageIndex) {
		String sql_list = PageUtil
				.getPageSQL(
						"SELECT T2.APPNAME,T3.NAME AS DEPTNAME,T1.* FROM T_SYS_SSO_LOG T1,T_SYS_APP_INFO T2,T_SYS_DEPT T3 WHERE T1.TARGETSYS =T2.APPID AND T1.DEPTID = T3.DEPTID ORDER BY LOGINTIME DESC",
						pageIndex);
		String sql_count = "SELECT COUNT(*) FROM T_SYS_SSO_LOG T1,T_SYS_APP_INFO T2,T_SYS_DEPT T3 WHERE T1.TARGETSYS =T2.APPID AND T1.DEPTID = T3.DEPTID";
		int _count = sjt.queryForInt(sql_count);
		List<SysSsoLog> list = sjt.query(sql_list, new SysSsoLogMapper());
		return new Pagination<SysSsoLog>(list, _count, pageIndex);
	}

	@Override
	public Pagination<SysSsoLog> findSsoInfoByConditions(String userid, String uname, String targetsys,
			String logintime1, String logintime2, int pageIndex) {
		StringBuilder sql_list = new StringBuilder(
				"SELECT T3.NAME AS DEPTNAME,T1.* FROM t_sys_login_log T1 ,T_SYS_DEPT T3 WHERE  T1.DEPTID = T3.DEPTID");
		StringBuilder sql_count = new StringBuilder(
				"SELECT COUNT(*) FROM t_sys_login_log T1,T_SYS_DEPT T3 WHERE  T1.DEPTID = T3.DEPTID");
		StringBuilder sql = new StringBuilder();
		if (StringUtils.hasText(userid)) {
			sql.append(" AND T1.USERID LIKE  '%" + userid + "%'");
		}
		if (StringUtils.hasText(uname)) {
			sql.append(" AND T1.UNAME LIKE  '%" + uname + "%'");
		}
		if (StringUtils.hasText(targetsys)) {
			sql.append(" AND T1.TARGETSYS ='" + targetsys + '\'');
		}
		if (StringUtils.hasText(logintime1) && StringUtils.hasText(logintime2)) {
			sql.append(" AND trunc(T1.LOGINTIME) BETWEEN TO_DATE('" + logintime1 + "','yyyy-MM-dd') AND TO_DATE('"
					+ logintime2 + " ','yyyy-MM-dd')");
		}
		System.out.println(sql_list);
		int _total = sjt.queryForInt(sql_count.append(sql).toString());
		List<SysSsoLog> list = sjt.query(
				PageUtil.getPageSQL(sql_list.append(sql).append(" ORDER BY LOGINTIME DESC").toString(), pageIndex),
				new SysSsoLogMapper());     
		return new Pagination<SysSsoLog>(list, _total, pageIndex);
	}

	@Override
	public void insertSysSsoLog(SysSsoLog log) {
		String sql = "INSERT INTO T_SYS_LOGIN_LOG(USERID,DEPTID,UNAME,LOGINTIME,LOGINIP) VALUES(?,?,?,sysdate,?)";
		sjt.update(sql, log.getUserid(), log.getDeptid(), log.getUname(),log.getLoginip());
	}
}
